package com.miaoxun.service.queue;


import org.apache.rocketmq.client.exception.MQBrokerException;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.remoting.exception.RemotingException;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


@Service("producer")
public class Producer {
    // 发送消息，destination是发送到的队列，message是待发送的消息


    DefaultMQProducer producer = ProducerManager.createProducer();

    public void send(String message, int time) {

        try {
            producer.start();
            Message msg = new Message("queue",
                    "queue",
                    "1",
                    message.getBytes());
            msg.setDelayTimeLevel(time);
            SendResult result = producer.send(msg);
            System.out.println("发送端：" + "id:" + result.getMsgId() +
                    " result:" + result.getSendStatus());
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            producer.shutdown();
        }

    }

}